package com.alibaba.citrus.cr.mn.order.center.facade.adapter.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BusinessChannelAbilityAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.response.BusinessChannelDTO;
import com.alibaba.cloud.retail.customer.center.ability.BusinessChannelAbilityService;
import com.alibaba.cloud.retail.customer.center.req.BusinessChannelListReq;
import com.alibaba.cloud.retail.customer.center.resp.QueryBusinessChannelResp;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lydass.bizphin.maca.model.Result;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName BusinessChannelAbilityAdapterImpl
 * @description: TODO
 * @date 2023/06/04 23:47
 * @version: 1.0
 */
@Service
public class BusinessChannelAbilityAdapterImpl implements BusinessChannelAbilityAdapter {
    private static final Log log = Log.getLogger(BusinessChannelAbilityAdapterImpl.class);

    @DubboReference
    private BusinessChannelAbilityService businessChannelAbilityService;

    @Override
    public List<BusinessChannelDTO> queryBusinessChannel(List<String> businessChannelCodeList) {
        if(CollectionUtils.isEmpty(businessChannelCodeList)) {
            return Lists.newArrayList();
        }
        BusinessChannelListReq businessChannelListReq = new BusinessChannelListReq();
        businessChannelListReq.setChannelCodeList(businessChannelCodeList);
        Result<List<QueryBusinessChannelResp>> result = businessChannelAbilityService.findBusinessChannelList(businessChannelListReq);

        log.info("业务渠道查询 request={}, result={}", JSON.toJSONString(businessChannelListReq), JSONObject.toJSONString(result));
        if (Objects.isNull(result) || Objects.isNull(result.getResult())) {
            return Lists.newArrayList();
        }

        return result.getResult().stream().map(p -> {
            BusinessChannelDTO businessChannelDTO = new BusinessChannelDTO();
            BeanUtils.copyProperties(p, businessChannelDTO);
            return businessChannelDTO;
        }).collect(Collectors.toList());
    }
}
